/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.synjones.pay.serv.recharge.service;

import com.synjones.pay.bank.api.IBankJump;
import com.synjones.pay.bank.wisedu.service.WisBankJumpImpl;
import com.synjones.pay.model.common.ResponseBean;
import com.synjones.pay.model.context.StatusInfo;
import com.synjones.pay.model.recharge.RechargeRespBean;
import com.synjones.pay.serv.dao.bean.RechargeDao;
import com.synjones.pay.serv.dao.repository.RechargeDaoRepository;
import com.synjones.pay.serv.recharge.helper.RechargeHelper;
import java.util.Map;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 *
 * @author hsd
 */
@Service("rechargeNoticeReceiverService")
public class RechargeNoticeReceiverServiceImpl implements IRechargeNoticeReceiverService {

    private final static Logger logger = LoggerFactory.getLogger(RechargeNoticeReceiverServiceImpl.class);

    @Resource
    private RechargeDaoRepository rechargeDao;

    @Override
    public void processNoticeData(final Map<String, String> reqMap) {
        new Thread() {
            @Override
            public void run() {
                //1.获取返回数据
                ResponseBean respBean = getPayResult(reqMap);
                //2.先判断数据库中该笔订单是否为成功，如果为成功状态则不写卡。
                if (respBean != null) {
                    //3.查询订单状态
                    RechargeDao dao = rechargeDao.queryByOrderId(respBean.getOrderNo());
                    if (dao != null) {
                        logger.info("订单[{}]数据库状态为{}",respBean.getOrderNo(),dao.getStatusCode());
                        if (!"0".equals(dao.getStatusCode())) {
                            if (respBean.getStatusCode().equals(StatusInfo._0000.code())) {
                                updateOrderStatus(respBean);
                                //写卡 TODO
                                 
                            } else {
                                updateOrderStatus(respBean);
                            }
                        }
                    }
                }
            }
        }.start();
    }

    @Override
    public boolean updateOrderStatus(ResponseBean respBean) {
        if (respBean != null) {
            String statusCode = (StatusInfo._0000.code().equals(respBean.getStatusCode())) ? "0" : "1";
            String completeTime = RechargeHelper.generateCurrTime();
            String paytoolCode = respBean.getBankCode();
            String orderId = respBean.getOrderNo();
            String amount = respBean.getAmount();
            try {
                return rechargeDao.updateOrderStatusInfo(statusCode, completeTime, paytoolCode, amount, orderId) >= 1;
            } catch (Exception e) {
                logger.error("充值更新订单[{}]失败", respBean.getOrderNo(), e);
                return false;
            }
        }
        logger.info("更新返回结果对象ResponseBean不能为空");
        return false;
    }

    @Override
    public ResponseBean getPayResult(Map<String, String> reqMap) {
        IBankJump bank = new WisBankJumpImpl();
        return bank.buildRespBean(reqMap);
    }

    @Override
    public RechargeRespBean processFrontNotice(Map<String, String> reqMap) {
        ResponseBean respBean = getPayResult(reqMap);
        return RechargeHelper.assemFrontRespBean(respBean);
    }

}
